package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecFamilyEditBean;
import cn.ffcs.uec.staff.model.UecFamily;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecFamilyEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecFamilyEditBean bean = new UecFamilyEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecFamily uecFamily;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecFamilyEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 性别
		List<NodeVo> nodeVoSexList = new ArrayList<NodeVo>();
		String[][] sex = { { "1001A1100000000006A9", "男" },
				{ "1001A1100000000006A8", "女" }, { "~", "其他" } };
		for (int i = 0; i < sex.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(sex[i][j]);
				nodeVo.setName(sex[i][j + 1]);
			}
			nodeVoSexList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef1(), nodeVoSexList);

		// 政治面貌
		List<NodeVo> nodeVoPolityList = new ArrayList<NodeVo>();
		String[][] polity = { { "1001A1100000000006D6", "中国共产党党员" },
				{ "1001A1100000000006D7", "中国共产党预备党员" },
				{ "1001A1100000000006D8", "中国共产主义青年团团员" },
				{ "1001A1100000000006DI", "群众" } };
		for (int i = 0; i < polity.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(polity[i][j]);
				nodeVo.setName(polity[i][j + 1]);
			}
			nodeVoPolityList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPolitics(), nodeVoPolityList);
		
		//家庭成员状态
		List<NodeVo> nodeVoStatusList = new ArrayList<NodeVo>();
		String[][] status = { { "1001A1100000000005Y9", "正常" },
				{ "1001A1100000000005YA", "已退休" },
				{ "1001A1100000000005YB", "已离休" },
				{ "1001A1100000000005YC", "已去世" } };
		for (int i = 0; i < status.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(status[i][j]);
				nodeVo.setName(status[i][j + 1]);
			}
			nodeVoStatusList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef2(), nodeVoStatusList);
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecFamilyEditWindow().setTitle("新增人员家庭信息");
			bean.getUecFamilyEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecFamilyEditWindow().setTitle("查看人员家庭信息");
			uecFamily = (UecFamily) arg.get("uecFamily");
			if (null != uecFamily) {
				PubUtil.fillBeanFromPo(uecFamily, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecFamilyEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecFamilyEditWindow().setTitle("编辑人员家庭信息");
			bean.getUecFamilyEditDiv().setHeight("30%");
			uecFamily = (UecFamily) arg.get("uecFamily");
			if (null != uecFamily) {
				PubUtil.fillBeanFromPo(uecFamily, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecFamily = UecFamily.newInstance();
			PubUtil.fillPoFromBean(bean, uecFamily);
			dateTransToString();
			uecFamily.setStaffId(uecStaffInfo.getStaffId());
			uecFamily.addOnly();
			Events.postEvent("onOK", this.self, uecFamily);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecFamily);
			dateTransToString();
			uecFamily.updateOnly();
			Events.postEvent("onOK", this.self, uecFamily);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecFamilyEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getMemName().getValue())) {
			return "请填写家庭成员姓名";
		} else if (StrUtil.isNullOrEmpty(bean.getMemRelation().getValue())) {
			return "请填写与本人关系";
		}else if (StrUtil.isNullOrEmpty(bean.getMemRelation().getValue())) {
			return "请填写与本人关系";
		}
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getMemBirthday().getValue())) {
			// 出生日期
			uecFamily.setMemBirthday(formatter.format(bean.getMemBirthday()
					.getValue()));
		}

	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 出生日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecFamily.getMemBirthday()) {
			bean.getMemBirthday().setValue(
					sdf.parse(uecFamily.getMemBirthday()));
		}
	}

}
